package pl.un4giv3n.mightvsmagic.utils;

import java.util.Date;

import pl.un4giv3n.mightvsmagic.entity.Action;
import pl.un4giv3n.mightvsmagic.entity.ActionType;
import pl.un4giv3n.mightvsmagic.entity.User;

/**
 * @author andrzejewskia
 * util for Actions
 */
public final class ActionUtil {

	private ActionUtil() {
	};

	/**
	 * @param currentAction - performed action
	 * @param user - user that performed the action
	 * @return true if the action has ended
	 */
	public static boolean actionEnded(Action currentAction, User user) {
		long time = currentAction.getStarted().getTime();
		long duration = !user.isPremium() ? currentAction.getActionType()
				.getDuration() : currentAction.getActionType()
				.getPremiumDuration();
		return time + duration < System.currentTimeMillis();
	}
	

	public static String getActionEnd(Action currentAction, User user) {
		Date started = currentAction.getStarted();
		long duration = !user.isPremium() ? currentAction.getActionType()
				.getDuration() : currentAction.getActionType()
				.getPremiumDuration();
		Date end = new Date(started.getTime() + duration);
		long now = System.currentTimeMillis();
		long minutesLeft = Math.round((end.getTime() - now)/(1000*60d));
		return minutesLeft +" min.";
	}
	
	public static String generateErrorMessage(Action currentAction, User user){
		StringBuilder builder = new StringBuilder();
		ActionType type = currentAction.getActionType();
		if(actionEnded(currentAction, user)){
			builder.append("Musisz najpierw zakończyc ");
			if(type == ActionType.TRAIN_FIGHT || type == ActionType.TRAIN_MAGIC){
				builder.append("trening. ");
			}else{
				builder.append("pracę. ");
			}
			return builder.toString();
		}
		if(type == ActionType.NO_ACTION){
			return builder.toString();
		} else if(type == ActionType.FIGHT){
			builder.append("Aktualnie odpoczywasz po walce. Spróbuj ponownie za około ");
		} else{
			builder.append("Jesteś w trakcie ");
			if(type == ActionType.TRAIN_FIGHT || type == ActionType.TRAIN_MAGIC){
				builder.append("treningu. ");
			}else{
				builder.append("pracy. ");
			}
			builder.append(" Spróbuj ponownie za około ");
		}
		builder.append(getActionEnd(currentAction, user));
		return builder.toString();
	}
	
	public static String generateStartMessage(Action currentAction, User user){
		StringBuilder builder = new StringBuilder();
		ActionType type = currentAction.getActionType();
		builder.append("Rozpoczynasz ");
		if(type == ActionType.TRAIN_FIGHT || type == ActionType.TRAIN_MAGIC){
			builder.append("trening. ");
		}else{
			builder.append("pracę. ");
		}
		builder.append(" Wróc ponownie za około ");
		builder.append(getActionEnd(currentAction, user));
		return builder.toString();
	}
	//this needs to be in format 12/31/2020 5:00 AM
	public static String getActionEndDate(Action action, User user) {
		long time = action.getStarted().getTime();
		long duration = !user.isPremium() ? action.getActionType()
				.getDuration() : action.getActionType()
				.getPremiumDuration();
		Date toFormat = new Date(time+duration);
		return FormatterUtil.formatDate(toFormat);
	}
	//this needs to be in format 12/31/2020 5:00 AM
	public static String getActionStartDate(Action action) {
		Date toFormat = new Date(action.getStarted().getTime());
		return FormatterUtil.formatDate(toFormat);
	}
}
